import pygame
import data

# TODO: all constants are duplicate with frame for now...
WIDTH = 800
HEIGHT = 600
FPS = 60

TEXTPADDING = 2

BLACK = (0, 0, 0);
WHITE = (255, 255, 255)
DARKGRAY = (50, 50, 50)
LIGHTGRAY = (100, 100, 100)

COLKEY_TRANSPARENT = (255, 0, 255)

TOOLTIPFG = WHITE
TOOLTIPBG = LIGHTGRAY
ELEMENTIDLE = DARKGRAY
ELEMENTACTIVE = LIGHTGRAY

FILL = 0
OUTLINE = 1

GAME_TITLE = "Worst Case"


def init_ui():
    global screen
    global font
    global bigFont
    global clock

    pygame.init()
    pygame.font.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    font = pygame.font.Font(data.filepath('freesansbold.ttf'), 12)
    bigFont = pygame.font.Font(data.filepath('freesansbold.ttf'), 24)
    pygame.display.set_caption("Worst Case")
    clock = pygame.time.Clock()
    
def draw_text(msg, atX, atY):
    renderedText = font.render(msg, True, WHITE, BLACK)
    screen.blit(renderedText, (atX, atY))

def draw_text_max_width(msg, atX, atY, max_width):
    num_lines = 0
    while len(msg) > 0:
        line = msg
        while font.size(line)[0] > max_width:
            line = line[:line.rfind(' ')]
        msg = msg[len(line):]
        renderedText = font.render(line, True, WHITE, BLACK)
        screen.blit(renderedText, (atX, atY))
        atY = atY + 15
        num_lines = num_lines+1
    return num_lines
	
def draw_text_big(msg, atX, atY):
    renderedText = bigFont.render(msg, True, WHITE, BLACK)
    screen.blit(renderedText, (atX, atY))
    
def draw_tooltip(msg, mousePos):
    atX, atY = mousePos
    atX += 10
    atY += 10
    
    textW, textH = font.size(msg)
    rect = atX, atY, textW + TEXTPADDING * 2 + 1, textH + TEXTPADDING * 2 + 1
    pygame.draw.rect(screen, TOOLTIPBG, rect, FILL)
    pygame.draw.rect(screen, TOOLTIPFG, rect, OUTLINE)
    renderedText = font.render(msg, True, TOOLTIPFG, TOOLTIPBG)
    screen.blit(renderedText, (atX + TEXTPADDING + 1, atY + TEXTPADDING + 1))

    
def end_frame():
    pygame.display.flip()
    clock.tick(FPS)
